package com.xdf.service.impl;

import com.xdf.dao.SysUserMapper;
import com.xdf.entity.SysUser;
import com.xdf.service.SysUserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Optional;

/**
 * @author chanchaw
 * @create 2025-08-22 9:21
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class SysUserServiceImpl implements SysUserService {
    private final SysUserMapper dao;
    @Override
    public SysUser get8Id(long id) {
        return dao.selectOneById(id);
    }

    @Override
    public SysUser get8Username(String username) {
        HashMap<String, Object> conditionMap = new HashMap<>();
        conditionMap.put("username", username);
        return dao.selectOneByMap(conditionMap);
    }

    /**
     * 检查用户名和密码是否匹配，成功则返回用户，相反：
     * 1. 提示账户不存在
     * 2. 提示密码错误
     * @param username
     * @param password
     * @return
     */
    @Override
    public SysUser checkPwd(String username, String password) {
        HashMap<String, Object> conditionMap = new HashMap<>();
        conditionMap.put("username", username);
        SysUser sysUser = dao.selectOneByMap(conditionMap);
        if(sysUser == null) throw new RuntimeException("没有用户：" + username);
        String s = Optional.ofNullable(sysUser.getPassword()).orElse("");
        if(!s.equalsIgnoreCase(password)) throw new RuntimeException("密码错误！");
        return sysUser;
    }

    @Override
    public SysUser insert(SysUser record) {
        int i = dao.insertSelective(record);
        return dao.selectOneById(record.getId());
    }
}
